
package de.swta.freezay.admin.category;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.controller.exceptions.NonexistentEntityException;
import de.swta.freezay.database.controller.exceptions.PreexistingEntityException;
import de.swta.freezay.database.dbEntities.Category;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

/**
 *
 * @author Jan
 */
public abstract class CategoryEditPanel extends Panel {

    /** Input form for category. */
    Form categoryForm;
    /** EditMode. If catergory is edited editMode is true. */
    boolean editMode;

    /**
     * Creates a new instance of CategoryEditPanel.
     *
     * @param id Id of the panel.
     * @param categoryId Category to edit
     * @param editMode If catergory is edited editMode is true.
     */
    public CategoryEditPanel(String id, int categoryId, boolean editMode) {
        super(id);

        this.editMode = editMode;
        init(categoryId);
    }


    /**
     * Initializes the panel with all components.
     *
     * @param categoryId Id of the category to edit.
     */
    private void init(int categoryId) {

        Category c;

        if (editMode) {
            /* if the category exists - load it. */
            c = JPAController.CategoryJpaController.findCategory(categoryId);
        } else {
            /* otherwise create it. */
            c = new Category();
        }

        categoryForm = new Form("catForm");

        categoryForm.add(new AjaxSubmitLink("saveLink") {

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                /* When submit is pressed the edited category is saved or created. */
                Category c = (Category) categoryForm.getModelObject();

                if (editMode) {
                    try {
                        JPAController.CategoryJpaController.edit(c);
                    } catch (NonexistentEntityException ex) {
                        Logger.getLogger(CategoryEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (Exception ex) {
                        Logger.getLogger(CategoryEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    try {
                        JPAController.CategoryJpaController.create(c);
                    } catch (PreexistingEntityException ex) {
                        Logger.getLogger(CategoryEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (Exception ex) {
                        Logger.getLogger(CategoryEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                CategoryEditPanel.this.confirmLinkClicked(target);
            }

            /**
             *  Handles the error messages.
             */
            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(this.getParent().get("feedback"));

                super.onError(target, form);
            }
        });

        categoryForm.setModel(new CompoundPropertyModel(c));
        categoryForm.add(new FeedbackPanel("feedback").setOutputMarkupId(true));
        categoryForm.add(new RequiredTextField("name"));
        add(categoryForm);
    }

    /**
     * Handles the actions after the confirm link was clicked.
     * @param target Request target.
     */
    public abstract void confirmLinkClicked(AjaxRequestTarget target);
}
